package 笔记.多线程;

/**
 * @author CHL
 * @ClassName: VolatileDemo
 * @Description: TODO
 * @date 2025/10/17 14:14
 * @Version 1.0
 */
public class VolatileDemo {


    public static volatile boolean flag = false;

    public static void main(String[] args) throws InterruptedException {


        // 多线程的编码三大特性：有序性、可见性、原子性

        new Thread(() -> {
            System.out.println("volatile关键字测试开始");
            while (!flag) {
//                System.out.println("进来了");
            }
            // 打印这行，说明跳出循环，flag修改成功
            System.out.println("——————success——————");
        }).start();

        Thread.sleep(1000);

        new Thread(() -> prepareDate()).start();

    }


    public static void prepareDate() {
        System.out.println("线程二进入。。。。。");

        flag = true;
        System.out.println("线程二结束。。。。");
    }

}
